<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/8
 * Time: 13:40
 */

namespace app\admin\controller;

use app\admin\model\Seo as SeoModel;
use think\Request;


class SeoController extends BaseController
{
    public function lst()
    {
        $params = $this->request->param();
        if(!empty($params['name'])){
            $name = "%".$params['name']."%";
        }else{
            $name = "%%";
        }
        $data = SeoModel::where('pagename_cn|pagename_en','like',$name)
            ->order('create_time','asc')
            ->paginate(8,false,["query"=>\request()->param()]);
        $page = $data->render();
        $this->assign([
            'page' => $page,
            'data' => $data,
        ]);
        return $this->fetch();
    }

    public function add()
    {
        $request = Request::instance();
        switch  ($request->method()){
            case "GET":
                return $this->fetch();
                break;
            case "POST":
                $params = $request->param();
                $model = new SeoModel();
                if($model->allowField(true)->save($params)){
                    $this->success('添加优化信息成功!',url('admin/seo/lst'),'',1);
                    exit;
                }
                else{
                    $this->error($model->getError());
                    exit;
                }
                break;
            default:
                return "错误的数据信息";
        }
    }

    public function edi()
    {
        $request = Request::instance();
        switch  ($request->method()){
            case "GET":
                $id = $request->param('id');
                $this->assign('data',SeoModel::find($id));
                return $this->fetch();
                break;
            case "POST":
                $params = $request->param();
                $model = new SeoModel();
                if($model->allowField(true)->isUpdate(true)->save($params)){
                    $this->success('修改优化信息成功!',url('admin/seo/lst'),'',1);
                    exit;
                }
                else{
                    $this->error($model->getError());
                    exit;
                }
                break;
            default:
                return "错误的数据信息";
        }
    }

    public function del()
    {
        $id = $this->request->param("id");
        if(SeoModel::destroy($id)){
            return '亲,您已删除成功!^o^';
        }
        return '删除失败了...';
    }
}